<?php
if( defined("_PARA_PARA") )
    return;
define( "_PARA_PARA", 1 );

class ParaPara {
    public $config;
    public $dblayer;
    public $account;
    public $token;
    public $remote_addr;
    public $now;

    private $forum         = null;
    private $chat          = null;
    private $ipblocks      = null;
    private $quote_manager = null;

    // flags to indicate how much is known about current user
    public $token_validated   = false;  // token was validated
    public $account_fetched   = false;  // account was fetched from DB

    // those will be set after the parse_auth_cookie method completes
    private $screen_name = null;
    private $pwd_md5;

    public $version = "0.01";

    public function ParaPara( $config, $dblayer) {
        $this->config  = $config;
        $this->dblayer = $dblayer;
        $this->now     = $dblayer->now();

        // if there is a token, it should be parsed and so on...

        $this->remote_addr = $_SERVER['REMOTE_ADDR'];

    }


    public function get_screen_name() {
        if($this->account_fetched)
            return $this->account->screen_name;
        if($token_validated)
            return $this->screen_name;
        return null;

    }


    public function get_uid() {
        if($this->account_fetched)
            return $this->account->uid;
        return null;
    }


    public function fetch_account() {
        if($this->account_fetched)
            return $this->account;

        $account = null;
        if($this->token_validated)
            $account = $this->dblayer->account_fetch_by_screenname( $this->screen_name );
        else
            return null;

        if($account == null)
            return null;

        $this->account         = $account;
        $this->account_fetched = true;

        return $this->account;
    }


    public function is_admin() {
        if($this->account && $this->account->is_admin() )
            return true;
        return false;
    }


    public function is_logged_in() {
        if($this->account && $this->account->is_logged_in() ) {
            return true;
        }
        return false;
    }


    public function is_ipblocked() {
        $this->read_ipblocks();
        if( !$this->ipblocks )
            return;

        $logged_in = $this->is_logged_in();

        if( $logged_in ) {
            foreach( $this->ipblocks as $b ) {
                if( $b->block_logged_in  && ($b->check_ip( $this->remote_addr) == 'BLOCKED' ) )
                    return $b;
            }
        }
        else {
            foreach( $this->ipblocks as $b ) {
                if( $b->check_ip( $this->remote_addr) == 'BLOCKED' )
                    return $b;
            }
        }
        return null;
    }


    private function read_ipblocks() {
        if( $this->ipblocks  == null )
            $this->ipblocks = $this->dblayer->get_ipblocks();
    }

    public function parse_auth_cookie( $cookie ) {
        $auth_cookie = json_decode( $cookie );

        // decoding went fine
        if($auth_cookie) {
            $token = $auth_cookie->token;

            if($token) {
                // fix the weird disappearing plusses:
                $token = str_replace( ' ', '+', $token );
                $stuff = Token::parse( $token );

                if($stuff['status'] == "ok") {
                    $this->token             = $token;
                    $this->screen_name       = $stuff['uname'];
                    $this->pwd_md5           = $stuff['pwd_md5'];
                    $this->token_validated   = true;
                    return true;
                }
            }
        }

        return false;
    }


    public function set_auth_cookie( $screenname, $pwd_md5 ) {
        $token = Token::generate($screenname, $pwd_md5, 3600 );
        
        $json_string  = json_encode( array( 'token' => $token ) );
        setcookie( 'ParaSite_Auth', $json_string, time() + 3600 );

    }

    public function renew_auth_cookie( ) {
        $this->set_auth_cookie( $this->screen_name, $this->pwd_md5 );
    }

    public function delete_auth_cookie( ) {
        setcookie( 'ParaSite_Auth', '', time() );
    }

    public function make_new_token( ) {
        if(!$this->token_validated)
            return null;

        return Token::generate($this->screen_name, 
                               $this->pwd_md5, 
                               3600 );
    }


    public function config_get( $section, $key) {
        return $this->config->get( $section, $key );
    }


    public function config_set_save( $section, $key, $value ) {
        $this->config->set( $section, $key, $value );
        $this->dblayer->save_config_value( $section, $key, $value );
    }


    public function get_forum( $topics ) {
        if($this->forum != null)
            return $this->forum;

        $this->forum = $this->dblayer->get_forum( $topics );
        return $this->forum;

    }
    
    public function get_thread( $thread_id ) {
        return $this->dblayer->get_forum_thread( $thread_id );
    }


    public function get_message_body( $msg_id ) {
        return $this->dblayer->get_forum_message_bodies( $msg_id );
    }


    public function get_chat( ) {
        if($this->chat != null)
            return $this->chat;

        $this->chat = $this->dblayer->get_chat(  );
        return $this->chat;

    }


    public function get_linkroll() {
        if($this->linkroll != null)
            return $this->linkroll;

        $this->linkroll = $this->dblayer->get_linkroll(  );
        return $this->linkroll;
    }


    public function quote_of_the_day() {
        
        if(!$this->quote_manager)
            $this->quote_manager = new QuoteManager( );

        $this->quote_manager->htmlize( array() );
    }


    public function mail_confirmation( $to, $uid, $act_code) {

        $subject  = "ParaCode Confirmation Mail";
        $template = require('include/confirmation_mail.php');

        $url = "http://mars.dyndns.dk/paraforum/v2/?sec=register&act_code=$act_code&uid=$uid";

        $mailtext = preg_replace('/__CONFIRMATION_URL__/', $url, $template);

        $subject = 'Account confirmation';

        mail( $to, $subject, $mailtext);
    }

}


?>